package com.menu.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.menu.Mapper.*;
import com.menu.Pojo.Menu;
import com.menu.Pojo.MenuFather;
import com.menu.Pojo.Notice;
import com.menu.Pojo.User;
import com.menu.Service.NoticeService;
import com.menu.Utils.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Resource
    NoticeMapper noticeMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    MenuMapper menuMapper;
    @Resource
    MenuFatherMapper menuFatherMapper;
    @Resource
    GoodsMapper goodsMapper;


    @Override
    public ResponseResult addCollectionMenuNotice(Integer id, Integer userOneId, Integer userTwoId) {
        User user2=userMapper.findByUserId(userTwoId);
        if(user2==null){
            return ResponseResult.error("不存在用户2");
        }
        Menu menu= menuMapper.findByMenuId(id);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        Notice notice=new Notice();
        notice.setNoticeState(0);
        notice.setNoticeTime(date1);
        notice.setUserOneId(userOneId);
        notice.setUserTwoId(userTwoId);
        notice.setId(id);
        notice.setMenuSrc(menu.getMenuSrc());
        noticeMapper.addNotice(notice);
        Notice notice1=noticeMapper.findByNoticeId(notice.getNoticeId());
        String content="收藏了你的菜谱  "+notice1.getNickname()+"收藏了你的菜谱";
        noticeMapper.updateContent(content,notice.getNoticeId());
        return ResponseResult.success("成功");
    }

    @Override
    public ResponseResult addCollectionMenuFatherNotice(Integer id, Integer userOneId, Integer userTwoId) {
        User user2=userMapper.findByUserId(userTwoId);
        if(user2==null){
            return ResponseResult.error("不存在用户2");
        }
        MenuFather menuFather= menuFatherMapper.findByMenuFatherId(id);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        Notice notice=new Notice();
        notice.setNoticeState(1);
        notice.setNoticeTime(date1);
        notice.setUserOneId(userOneId);
        notice.setUserTwoId(userTwoId);
        notice.setId(id);
        notice.setMenuSrc(menuFather.getMenuFatherSrc());
        noticeMapper.addNotice(notice);
        Notice notice1=noticeMapper.findByNoticeId(notice.getNoticeId());
        String content="收藏了你的菜单  "+notice1.getNickname()+"收藏了你的菜单";
        noticeMapper.updateContent(content,notice.getNoticeId());
        return ResponseResult.success("成功");
    }

    @Override
    public ResponseResult addAttentionUserNotice(Integer userOneId, Integer userTwoId) {
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        Notice notice=new Notice();
        notice.setNoticeState(2);
        notice.setNoticeTime(date1);
        notice.setUserOneId(userOneId);
        notice.setUserTwoId(userTwoId);
        notice.setId(userOneId);
        noticeMapper.addNotice(notice);
        Notice notice1=noticeMapper.findByNoticeId(notice.getNoticeId());
        String content="关注了你  "+notice1.getNickname()+"关注了你";
        noticeMapper.updateContent(content,notice.getNoticeId());
        return ResponseResult.success("成功");
    }

    @Override
    public ResponseResult addThumbsUpMenuNotice(Integer id, Integer userOneId, Integer userTwoId) {
        Menu menu= menuMapper.findByMenuId(id);
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        Notice notice=new Notice();
        notice.setNoticeState(3);
        notice.setNoticeTime(date1);
        notice.setUserOneId(userOneId);
        notice.setUserTwoId(userTwoId);
        notice.setId(id);
        notice.setMenuSrc(menu.getMenuSrc());
        noticeMapper.addNotice(notice);
        Notice notice1=noticeMapper.findByNoticeId(notice.getNoticeId());
        String content="点赞了你的食谱  "+notice1.getNickname()+"点赞了你的食谱";
        noticeMapper.updateContent(content,notice.getNoticeId());
        return ResponseResult.success("成功");
    }

    @Override
    public ResponseResult addNotice(String content,Integer userTwoId) {
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1=simpleDateFormat.format(date);
        Notice notice=new Notice();
        notice.setNoticeState(4);
        notice.setUserTwoId(userTwoId);
        notice.setNoticeTime(date1);
        notice.setContent(content);
        noticeMapper.addNotice(notice);
        return ResponseResult.success("成功");
    }


    @Override
    public ResponseResult findCollectionAndThumbsUpNotice(Integer userId,Integer pageNow,Integer pageSize) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("不存在用户");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<Notice> noticeList=noticeMapper.findCollectionAndThumbsUpNoticeByUserIdOrderByNoticeTime(userId);
        PageInfo<Notice> pageInfo=new PageInfo<>(noticeList);
        return ResponseResult.success("查找收藏食谱,收藏菜单,点赞食谱的消息通知成功",pageInfo);
    }


    @Override
    public ResponseResult findAttentionNotice(Integer userId,Integer pageNow,Integer pageSize) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("不存在用户");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<Notice> noticeList=noticeMapper.findAttentionNoticeByUserIdOderByNoticeTime(userId);
        PageInfo<Notice> pageInfo=new PageInfo<>(noticeList);
        return ResponseResult.success("查找关注用户的消息通知成功",pageInfo);
    }

    @Override
    public ResponseResult findOfficialNoticeByUserIdOrderByNoticeTime(Integer userId,Integer pageNow,Integer pageSize) {
        User user= userMapper.findByUserId(userId);
        if(user==null){
            return ResponseResult.error("不存在用户");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<Notice> noticeList=noticeMapper.findOfficialNoticeByUserIdOrderByNoticeTime(userId);
        PageInfo<Notice> pageInfo=new PageInfo<>(noticeList);
        return ResponseResult.success("查找官方通知成功",pageInfo);
    }

    @Override
    public ResponseResult deleteNotice(Integer noticeId) {
        Notice notice= noticeMapper.findByNoticeId(noticeId);
        if(notice==null){
            return ResponseResult.error("通知不存在");
        }
        noticeMapper.deleteNotice(noticeId);
        return ResponseResult.success("删除成功");
    }

    @Override
    public ResponseResult batchDeleteNotice(Integer[] noticeId) {
        noticeMapper.batchDeleteNotice(noticeId);
        return ResponseResult.success("成功");
    }

}
